<script setup>
import { StarFilled, ShoppingCart, ArrowDown } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { itemGetOneService } from '@/api/item'
import { cartAdd } from '@/api/cart'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

const route = useRoute()
const itemDetail = ref({})
const name = ref('')

const productData = ref({
  颜色: '',
  尺寸: ''
})

const cart = () => {
  router.push('/cart')
}

onMounted(async () => {
  // console.log(route.params.id)
  const res = await itemGetOneService(route.params.id)
  itemDetail.value = res.data.data
  console.log(itemDetail.value.spec)
  name.value = userStore.user.username || '亲，请登录'
  productData.value = JSON.parse(res.data.spec)
})

const quantity = ref(1)

const increaseQuantity = () => {
  quantity.value += 1
}

const decreaseQuantity = () => {
  if (quantity.value > 1) {
    quantity.value -= 1
  }
}

const main = () => {
  router.push('/')
}

const addCart = async () => {
  const itemInfo = ref({
    itemId: itemDetail.value.id,
    name: itemDetail.value.name,
    spec: itemDetail.value.spec,
    price: itemDetail.value.price,
    image: itemDetail.value.image
  })
  await cartAdd(itemInfo.value)
  ElMessage({
    message: '加入购物车成功',
    type: 'success'
  })
  router.push('/cart')
}
</script>

<template>
  <div class="container">
    <div class="taobao-header">
      <div class="top-bar">
        <span class="highlight" @click="login">{{ name }}</span
        ><el-icon class="xia"><ArrowDown /></el-icon>
        <span>手机逛淘宝</span>
        <span>网页无障碍</span>
        <span class="separator">|</span>
        <span @click="main" style="cursor: pointer">微购首页</span>
        <span>我的淘宝</span><el-icon class="xia"><ArrowDown /></el-icon>
        <span @click="cart"
          ><el-icon class="shop"><ShoppingCart /></el-icon>购物车</span
        >
        <el-icon><StarFilled /></el-icon><span>收藏夹</span><el-icon class="xia"><ArrowDown /></el-icon>
        <span>商品分类</span>
        <span>免费开店</span><el-icon class="xia"><ArrowDown /></el-icon>
        <span
          >千牛卖家中心<el-icon><ArrowDown class="xia" /></el-icon></span
        ><el-icon class="xia"><ArrowDown /></el-icon> <span>帮助中心</span><el-icon class="xia"><ArrowDown /></el-icon>
      </div>
      <div class="logo-search-nav">
        <div class="logo">
          <img src="@/assets/logo.png" alt="logo" class="logo-img" @click="main" />
        </div>
        <div class="banner-placeholder"></div>
      </div>
    </div>

    <div class="product-page">
      <div class="left-section">
        <img :src="itemDetail.image" alt="" class="product-video" />
      </div>
      <div class="right-section">
        <div class="product-name">{{ itemDetail.name }}</div>
        <div class="product-price">
          <span class="current-price">{{ (itemDetail.price / 100).toFixed(2) }}</span>
        </div>
        <div class="product-shipping">
          配送: 广东揭阳 至 佛山 顺德
          <br />
          快递: 免运费 预计5小时内发货 | 承诺48小时内发货
        </div>
        <div class="product-guarantee">保障: 退货运费险 7天无理由退货</div>
        <div class="product-color">
          <div class="label">颜色:</div>
          <div class="color-options">
            <button class="color-option">{{ productData.颜色 || '暂无可选颜色' }}</button>
            <!-- Add more color options as needed -->
          </div>
        </div>
        <div class="product-size">
          <div class="label">尺码:</div>
          <div class="size-options">
            <button class="size-option">{{ productData.尺寸 || '暂无可选尺码' }}</button>
            <!-- Add more size options as needed -->
          </div>
        </div>
        <div class="product-quantity">
          <div class="label">数量:</div>
          <div class="quantity-control">
            <button @click="decreaseQuantity">-</button>
            <input type="text" v-model="quantity" readonly />
            <button @click="increaseQuantity">+</button>
          </div>
        </div>

        <div class="product-buttons">
          <button class="buy-now-button">立即购买</button>
          <button class="add-to-cart-button" @click="addCart()">加入购物车</button>
        </div>
      </div>
    </div>

    <div class="user-reviews">
      <div class="reviews-header">
        <span class="active-tab">用户评价</span>
      </div>
      <div class="reviews-content">
        <div class="review">
          <div class="review-user">
            <img src="@/assets/th.jpg" alt="User Avatar" class="user-avatar" />
            <div class="user-info">
              <div class="username">h**i</div>
              <div class="review-details">24天前 · 白灰色</div>
            </div>
          </div>
          <div class="review-text">是纯棉的摸起来很舒服！造型设计简洁大方！非常满意全五星支持推荐大家来买，卖家发货快服务好已经收藏店铺打算以后常来</div>
          <div class="review-images">
            <!-- <img src="image1-url" alt="Review Image 1" />
          <img src="image2-url" alt="Review Image 2" />
          <img src="image3-url" alt="Review Image 3" /> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.container {
  padding: 0 95px;
}

.taobao-header {
  width: 100%;
  background-color: #f5f5f5;
}

.top-bar {
  display: flex;
  justify-content: flex-start;
  background-color: #e8e6e2;
  padding: 5px 20px;
  padding-left: 50px;
  border-bottom: 1px solid #eee;
}

.top-bar span {
  margin-right: 8px;
  color: #666;
  font-size: 12.5px;
}
.top-bar .highlight {
  margin-left: 60px;
  color: red;
}

.top-bar .separator {
  color: #fff;
  margin: 0 150px;
}
.logo-search-nav {
  display: flex;
  align-items: center;
  padding: 5px 0;
  background-color: #e8e6e2;
}

.logo {
  margin-right: 80px;
  margin-left: 60px;
}

.logo-img {
  height: 70px;
}

.nav-links span {
  font-size: 12px;
  margin-left: 15px;
  color: #65738a;
}

.banner-placeholder {
  width: 200px;
  height: 100px;
  background-color: #e8e6e2;
  text-align: center;
  line-height: 100px;
  color: #666;
  margin-left: 30px;
}
.icon,
.shop {
  margin-right: 5px;
}
.xia {
  font-size: 10px;
  margin-top: 4px;
  margin-right: 10px;
}

.product-page {
  display: flex;
  padding: 20px;
  padding-left: 150px;
  gap: 20px;
}

.left-section {
  margin-right: 100px;
  flex: 1;
}

.product-video {
  width: 400px;
  border-radius: 10px;
}

.right-section {
  flex: 2;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.product-name {
  font-size: 24px;
  font-weight: bold;
}

.product-price {
  font-size: 20px;
  color: #ff6600;
}

.current-price {
  font-size: 28px;
  font-weight: bold;
  color: #ff6600;
}

.product-shipping,
.product-guarantee {
  font-size: 14px;
  color: #333;
}

.product-color,
.product-size,
.product-quantity {
  display: flex;
  align-items: center;
  gap: 10px;
}

.label {
  font-weight: bold;
}

.color-options,
.size-options,
.quantity-control {
  display: flex;
  gap: 10px;
}

.color-option,
.size-option,
.quantity-control button {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  cursor: pointer;
}

.color-option:hover,
.size-option:hover,
.quantity-control button:hover {
  border-color: #ff6600;
}

.quantity-control input {
  width: 50px;
  text-align: center;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.product-buttons {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.buy-now-button,
.add-to-cart-button {
  /* flex: 1; */
  padding: 15px;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  color: white;
}

.buy-now-button {
  width: 200px;
  background: linear-gradient(to right, #ff6600, #ff9900);
}

.add-to-cart-button {
  background: linear-gradient(to right, #ff9900, #ffcc00);
  width: 200px;
}

.user-reviews {
  margin-top: 30px;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.reviews-header {
  display: flex;
  gap: 20px;
  font-size: 16px;
  margin-bottom: 20px;
}

.reviews-header span {
  cursor: pointer;
}

.active-tab {
  color: #ff6600;
  border-bottom: 2px solid #ff6600;
}

.reviews-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.review {
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
}

.review-user {
  display: flex;
  gap: 10px;
  align-items: center;
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.user-info {
  display: flex;
  flex-direction: column;
}

.username {
  font-weight: bold;
}

.review-details {
  font-size: 12px;
  color: #999;
}

.review-text {
  margin-top: 10px;
  font-size: 14px;
}

.review-images {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.review-images img {
  width: 100px;
  height: 100px;
  border-radius: 5px;
  object-fit: cover;
}
</style>
